
package monitortest;

//import gnu.io.CommPortIdentifier;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;


/**
 *
 * @author the Dope Factory
 */
public class InterfacePort {
    
    /** Combo box per la selezione della porta */
    JComboBox cmbPort;
    
    /** JDialog per la scelta della COM e l'avvio del monitoring*/
    JDialog startCom;
    
    /**Button per connessione se porta COM individuata*/
    JButton connect;
    Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
    
    public String comValue="";
    
    public InterfacePort(){
        //JDialog con scelta porta com
        startCom=new JDialog();
        startCom.setModal(true);
        startCom.setIconImage(Interface.favicon);
        startCom.setBounds((scr.width-300)/2, (scr.height-100)/2, 300, 100);
        startCom.setLayout(null);
        startCom.setTitle("Serial Connection");
        startCom.setResizable(false);
        JLabel intro=new JLabel("Usa la porta seriale: ");
        connect=new JButton("Start");
        JButton cancel=new JButton("Cancel");
        connect.setEnabled(false);
        cmbPort = new JComboBox();

        startCom.add(cmbPort);
        startCom.add(intro);
        startCom.add(connect);
        startCom.add(cancel);
        intro.setBounds(10,5,150,30);
        cmbPort.setBounds(130,10,100,20);
        connect.setBounds(30,40,100,25);
        cancel.setBounds(130,40,100,25);
        
//        startCom.setVisible(true);
        
        cmbPort.addActionListener(new ActionListener(){
        @Override
            public void actionPerformed(ActionEvent e){
                if(cmbPort.getSelectedItem().toString()!=null){
                    connect.setEnabled(true);
                } 
            }
        });
        
        connect.addActionListener(new ActionListener(){
        @Override
            public void actionPerformed(ActionEvent e){
                //starting connection
//                SerialRead serial=new SerialRead();
//                if(cmbPort.getSelectedItem().toString()!=null){
//                    comValue=cmbPort.getSelectedItem().toString();
//                    serial.portInit(cmbPort.getSelectedItem().toString());
//                    
//                    LedManager led=new LedManager();
//                    led.run();
//                    
//                    startCom.dispose();
//                }                
            }
        });
        cancel.addActionListener(new ActionListener(){
        @Override
            public void actionPerformed(ActionEvent e){
                startCom.dispose();
            }
        });
        
        portList();
        startCom.setVisible(true);
////           startCom.setIconImage(Pannellozzo.img);
    }
    
    /**
     * Metodo che prepara il combo box con le scelte delle porte COM disponibili
     */
    private void portList(){
//        cmbPort.removeAllItems();
//        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//
//        // Scandisce tutte le porte presenti per caricare il combo
//        while (portEnum.hasMoreElements())
//        {
//          CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
//          cmbPort.addItem(currPortId.getName());
//        }
    }   
}